package com.cn.yunhe.poi;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.cn.yunhe.mybatis.model.UserModel;

public class ExportExcel {
	 UserModel user=new UserModel();
	public void Export(String excelName, List<UserModel> list){
	        // 声明一个工作薄
	        HSSFWorkbook wb = new HSSFWorkbook();
	        //声明一个单子并命名
	        HSSFSheet sheet = wb.createSheet(excelName);
	        //给单子名称一个长度
	        sheet.setDefaultColumnWidth((short)15);
	        // 生成一个样式  
	        HSSFCellStyle style = wb.createCellStyle();
	        //创建第一行（也可以称为表头）
	        HSSFRow row = sheet.createRow(0);
	        //样式字体居中
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	        //给表头第一行一次创建单元格
	        HSSFCell cell = row.createCell((short) 0);
	        cell.setCellValue("编号"); 
	        cell.setCellStyle(style);
	        cell = row.createCell( (short) 1);  
	        cell.setCellValue("名称");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 2);  
	        cell.setCellValue("密码");  
	        cell.setCellStyle(style); 
	         
	     // 添加一些数据，这里先写死，大家可以换成自己的集合数据

			// 向单元格里填充数据
			for (short i = 0; i < list.size(); i++) {

				row = sheet.createRow(i + 1);
				row.createCell(0).setCellValue(list.get(i).getUserId());
				row.createCell(1).setCellValue(list.get(i).getUserName());
				row.createCell(2).setCellValue(list.get(i).getPwdword());
				row.createCell(3).setCellValue(list.get(i).getSex());
				row.createCell(4).setCellValue(list.get(i).getTelephone());
			}

			try {
				// 默认导出到F盘下
				FileOutputStream out = new FileOutputStream("F:user.xls");
				wb.write(out);
				out.close();

			} catch (FileNotFoundException e) {

				e.printStackTrace();
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
	}
